package se.mbaeumer.jarvisualizer.util;

import java.util.Collections;

import se.mbaeumer.jarvisualizer.core.model.AbstractEdge;
import se.mbaeumer.jarvisualizer.core.model.Graph;

public class GraphUtil {
	@SuppressWarnings("unchecked")
	public static Graph createEdgeNumberVector(Graph graph){
		Collections.sort(graph.getEdges());
		int[] edgeNumberVector = new int[graph.getNodes().size() + 1];
		for (int i=0;i<graph.getNodes().size();i++){
			edgeNumberVector[i] = -1;
		}
		
		edgeNumberVector[graph.getNodes().size()] = graph.getEdges().size();
		
		int oldValue = -1;
		int currentPosition = 0;
		
		for (AbstractEdge edge : graph.getEdges()){
			if (edge.getStartNode() != oldValue){
				edgeNumberVector[edge.getStartNode()] = currentPosition;
				oldValue = edge.getStartNode();
			}
			currentPosition++;
		}
		
		int i = edgeNumberVector.length-2;
		while (i>0){
			if (edgeNumberVector[i] == -1){
				edgeNumberVector[i] = edgeNumberVector[i+1];
			}
			i--;
		}
		
		graph.setEdgeNumberVector(edgeNumberVector);
		return graph;		
	}
}
